package 功能实现.管理员角色权限三级.service.impl;

import cn.tedu.autoshow.commons.ex.ServiceException;
import cn.tedu.autoshow.commons.pojo.vo.PageData;
import cn.tedu.autoshow.commons.util.PageInfoToPageDataConverter;
import cn.tedu.autoshow.commons.web.ServiceCode;
import cn.tedu.autoshow.passport.mapper.PermissionMapper;
import cn.tedu.autoshow.passport.mapper.RolePermissionMapper;
import cn.tedu.autoshow.passport.pojo.entity.Permission;
import cn.tedu.autoshow.passport.pojo.entity.RolePermission;
import cn.tedu.autoshow.passport.pojo.param.PermissionAddNewParam;
import cn.tedu.autoshow.passport.pojo.param.PermissionUpdateParam;
import cn.tedu.autoshow.passport.pojo.vo.PermissionListItemVO;
import cn.tedu.autoshow.passport.pojo.vo.PermissionStandardVO;
import cn.tedu.autoshow.passport.service.IPermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper mapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public void addNew(PermissionAddNewParam permissionAddNewParam) {
        log.info("开始:【新增权限】");
        log.debug("传入参数,permissionAddNewParam={}", permissionAddNewParam);
        //检查权限名称是否被占用，如果被占用，则抛出异常
        QueryWrapper<Permission> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("name",permissionAddNewParam.getName());
        int countByName = mapper.selectCount(queryWrapper2);
        log.info("规则：根据权限名称检索数据库结果,countByName={}", countByName);
        if(countByName > 0){
            log.info("结果2:不满足权限新增规则【权限名不可重复】，抛出自定异常...");
            throw new ServiceException(ServiceCode.ERR_CONFLICT, "添加权限失败，权限名已存在！");
        }

        log.info("满足权限新增规则，开始新增...");
        log.info("新增权限数据...");
        //名称未占用，添加权限到数据库
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionAddNewParam,permission);
        permission.setGmtCreate(LocalDateTime.now()).setGmtModified(LocalDateTime.now());

        log.debug("新增权限数据,permission={}", permission);
        int rows = mapper.insert(permission);
        if(rows != 1){
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", 1, rows);
            String message = "添加权限失败，服务器忙，请稍后再试！";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.info("结果1:新增权限结果,rows={}", rows);
    }

    @Override
    public void deleteById(Long id) {
        log.info("开始:【删除权限】");
        log.debug("传入参数,id={}", id);
        //根据权限ID检查权限是否存在数据，如果不存在，则抛出异常
        QueryWrapper<Permission> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("id", id);
        int countById = mapper.selectCount(queryWrapper2);
        log.info("规则：根据权限id检索数据库结果,countById={}", countById);
        if(countById == 0){
            log.info("结果2:根据权限id检索数据量为0，抛出异常...");
            String message = "删除权限失败，权限数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        //根据权限ID检查权限是否被使用，如果已被使用，则抛出异常
        QueryWrapper<RolePermission> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("permission_id", id);
        countById = rolePermissionMapper.selectCount(queryWrapper3);
        log.info("规则：根据权限id检索数据库结果,countById={}", countById);
        if(countById != 0){
            log.info("结果3:根据权限id检索数据量不为0，抛出异常...");
            String message = "删除权限失败，权限已被使用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        log.info("删除权限数据...");
        int rows = mapper.deleteById(id);
        if(rows != 1){
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", 1, rows);
            String message = "删除权限失败，服务器忙，请稍后再试！";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        log.info("结果1:删除权限结果,rows={}", rows);
    }

    @Override
    public void updateInfoById(Long id, PermissionUpdateParam permissionUpdateParam) {
        log.info("开始:【更新权限信息】");
        log.debug("传入参数,id={}", id);
        //检查权限ID检查权限是否存在数据，如果不存在，则抛出异常
        QueryWrapper<Permission> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("id", id);
        int countById = mapper.selectCount(queryWrapper2);
        log.info("规则：根据权限id检索数据库结果,countById={}", countById);
        if(countById == 0){
            log.info("结果2:根据权限id检索数据量为0，抛出异常...");
            String message = "更新权限失败，权限数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        //检查权限名称是否被占用，如果被占用，则抛出异常
        QueryWrapper<Permission> queryWrapper3 = new QueryWrapper<>();
        queryWrapper2.eq("name",permissionUpdateParam.getName());
        queryWrapper2.ne("id",id);
        int countByName = mapper.selectCount(queryWrapper2);
        log.info("规则：根据权限名称检索数据库结果,countByName={}", countByName);
        if(countByName > 0){
            log.info("结果3:不满足权限新增规则【权限名不可重复】，抛出自定异常...");
            String message = "修改权限失败，权限名已存在！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionUpdateParam, permission);
        permission.setId(id);
        int rows = mapper.updateById(permission);
        if(rows != 1){
            log.info("结果1:数据返回行数不满足预估行数，预估={}, 实际={}, 抛出自定异常...", 1, rows);
            String message = "编辑权限失败，服务器忙，请稍后再试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
        log.info("结果1:根据id编辑权限结果,rows={}", rows);
    }

    @Override
    public PermissionStandardVO getStandardById(Long id) {
        log.info("开始:【根据ID查询权限】");
        log.debug("传入参数,id={}", id);
        PermissionStandardVO standardById = mapper.getStandardById(id);

        //根据权限ID检查权限是否存在数据，如果不存在，则抛出异常
        if(standardById==null){
            log.info("结果2:根据id查询详情，结果为null，抛出异常...");
            String message = "查询权限详情失败，权限数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        log.info("结果1:查询出数据,standardById");
        return standardById;
    }

    @Override
    public PageData<PermissionListItemVO> list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum, pageSize);
    }

    @Override
    public PageData<PermissionListItemVO> list(Integer pageNum, Integer pageSize) {
        log.info("开始:【查询-权限-分页】");
        log.debug("传入参数, pageNum={}, pageSize={}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<PermissionListItemVO> list = mapper.list();
        PageInfo<PermissionListItemVO> pageInfo = new PageInfo<>(list);
        PageData<PermissionListItemVO> pageData = PageInfoToPageDataConverter.converter(pageInfo);
        log.debug("结果1: 查询结果：pageData={}", pageData);
        return pageData;
    }
}
